/*
 * main.c
 *
 *  Created on: 08.06.2011
 *      Author: alexs
 */

#include <avr/io.h>
#include "stk500protocol.h"
#define USARTBAUD 115200L

void jmp_0(void) __attribute__ ((naked));
void jmp_0(void) {
	asm volatile ("jmp 0\n\t"::);
}

void usart_init(void) __attribute__ ((naked)) __attribute__ ((section (".init7")));
void usart_init() {
	UCSRA=(1<<U2X);
	UCSRB=(1<<RXEN)|(1<<TXEN);
	UCSRC=(1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0);
	UBRRL=F_CPU / (8L * USARTBAUD) -1;
}

void reset_source_check(void) __attribute__ ((naked)) __attribute__ ((section (".init1")));
void reset_source_check() {
	if (MCUCSR&(1<<EXTRF)) return;
	if (MCUCSR&(1<<PORF)) return;
	jmp_0();
}

int main(){
	uint32_t dcount=(F_CPU/24L)*5L;

	DDRB=(1<<PB0)|(1<<PB1)|(1<<PB2);
	PORTB|=(1<<PB0);

	for (;;) {
		if ((UCSRA&(1<<RXC))>0) {
			stk_buff_add(UDR);
		}
		if (stk_communicate==0) {
			dcount--;
			if (dcount==0) jmp_0();
		} else {
			dcount=(F_CPU/24L)*30L;
		}
	}
	return 0;
}
